package com.zhoug.common3.utils.file;

import android.app.Application;
import android.content.Context;

import com.zhoug.common3.utils.StringUtils;

import java.io.File;

/**
 * app文件管理器
 *
 * @Author 35574
 * @Date 2021/11/22
 * @Description
 */
public class ZFileManager {
    private static final String TAG = ">>>ZFileManager";
    /**
     * ApplicationContext
     */
    private static Context mContext;
    /**
     * 文件默认是存储在外部存储空间还是内部存储空间,都是私有的别的应用不能访问
     * 优先级大于{@link #isPublic}
     */
    private static boolean isExternal = true;

    /**
     * 文件存储在外部,别的应用可以访问
     * 优先级小于{@link #isExternal}
     * 别的应用可以访问需要设置:
     * {@link #isExternal}:true,
     * {@link #isPublic}:true
     */
    private static boolean isPublic = true;

    /**
     * 适用于{@link #getExternalPackageDir}方法
     * 指定更具包名创建外部别的应用可访问的目录的父级目录
     * 例:包名是com.heking.mclzfirm 创建的目录是 heking/com.heking.mclzfirm
     */
    private static String externalPackageParentDir = "heking/";

    /**
     * 图片保存、缓存地址
     */
    public static String IMG_PATH = "files/image/";
    /**
     * 视频保存,缓存地址
     */
    public static String VIDEO_PATH = "files/video/";
    /**
     * 音频保存,缓存地址
     */
    public static String AUDIO_PATH = "files/audio/";
    /**
     * 文档保存,缓存地址
     */
    public static String DOC_PATH = "files/doc/";
    /**
     * 文档保存,缓存地址
     */
    public static String DOWNLOAD_PATH = "download/";

    /**
     * 更新包保存路径
     */
    public static String APK_PATH_INSTALL = "download/apk/";


    /**
     * 需要在{@link Application#onCreate()}中初始化
     *
     * @param appContext ApplicationContext
     */
    public static void init(Context appContext) {
        mContext = appContext;
    }

    private static Context getContext() {
        return mContext;
    }

    public static boolean isIsExternal() {
        return isExternal;
    }

    public static void setIsExternal(boolean isExternal) {
        ZFileManager.isExternal = isExternal;
    }

    public static boolean isIsPublic() {
        return isPublic;
    }

    public static void setIsPublic(boolean isPublic) {
        ZFileManager.isPublic = isPublic;
    }

    public static String getExternalPackageParentDir() {
        return externalPackageParentDir;
    }

    public static void setExternalPackageParentDir(String externalPackageParentDir) {
        if(externalPackageParentDir!=null){
            if(!externalPackageParentDir.endsWith("/")){
                externalPackageParentDir=externalPackageParentDir+"/";
            }
        }
        ZFileManager.externalPackageParentDir = externalPackageParentDir;
    }

    /**
     * 获取根目录:data/data/package/
     * 文件默认私有的，即别的应用不能访问，本应用卸载则文件删除
     *
     * @return
     */
    public static File getAppDataRoot() {
        return ZFileUtils.getAppDataRoot(getContext());
    }


    /**
     * 获取根目录下的子目录:data/data/package/dirName
     *
     * @return
     */
    public  static File getAppDataFolder(String dirName) {
        return ZFileUtils.getAppDataFolder(getContext(), dirName);
    }

    /**
     * 获取App外部储存根目录 android/data/package
     * 只能本应用访问，应用删除文件删除
     *
     * @return
     */
    public static  File getAppExternalRoot() {
        return ZFileUtils.getAppExternalRoot(getContext());
    }

    /**
     * 获取App外部储存根目录的子目录 android/data/package/dirName
     *
     * @return
     */
    public  static File getAppExternalFolder(String dirName) {
        return ZFileUtils.getAppExternalFolder(getContext(), dirName);
    }

    /**
     * 外部存储根目录,别的应用可以访问
     *
     * @return
     */
    public  static File getExternalRoot() {
        return ZFileUtils.getExternalRoot();
    }

    /**
     * 外部存储根目录,别的应用可以访问
     *
     * @return
     */
    public static  File getExternalFolder(String folder) {
        return ZFileUtils.getExternalFolder(folder);
    }


    /**
     * 更具包名创建外部目录,别的应用可以访问
     *
     * @return
     */
    public  static File getExternalPackageDir() {
        String dir;
        if (!StringUtils.isNullOrEmpty(externalPackageParentDir)) {
            dir = externalPackageParentDir + getContext().getPackageName();
        } else {
            dir = getContext().getPackageName();
        }
        return getExternalFolder(dir);
    }

    /**
     * 更具包名创建外部目录,别的应用可以访问
     *
     * @return
     */
    public  static File getExternalPackageDir(String dir) {
        File externalPackageDir = getExternalPackageDir();
        if (null != externalPackageDir) {
            return ZFileUtils.createDirectory(externalPackageDir, dir);
        }
        return null;
    }


    /**
     * 图片保存、缓存地址
     *
     * @return
     */
    public  static File getImageDir(boolean external, boolean isPublic) {
        if (external) {
            if (isPublic) {
                return getExternalPackageDir(IMG_PATH);
            }
            return getAppExternalFolder(IMG_PATH);
        } else {
            return getAppDataFolder(IMG_PATH);
        }
    }

    /**
     * 图片保存、缓存地址
     *
     * @return
     */
    public  static File getImageDir() {
        return getImageDir(isExternal, isPublic);
    }

    /**
     * 视频保存、缓存地址
     *
     * @return
     */
    public static File getVideoDir(boolean external, boolean isPublic) {
        if (external) {
            if (isPublic) {
                return getExternalPackageDir(VIDEO_PATH);
            }
            return getAppExternalFolder(VIDEO_PATH);
        } else {
            return getAppDataFolder(VIDEO_PATH);
        }
    }

    /**
     * 视频保存、缓存地址
     *
     * @return
     */
    public static File getVideoDir() {
        return getVideoDir(isExternal, isPublic);
    }

    /**
     * 音频保存、缓存地址
     *
     * @return
     */
    public static File getAudioDir(boolean external, boolean isPublic) {
        if (external) {
            if (isPublic) {
                return getExternalPackageDir(AUDIO_PATH);
            }
            return getAppExternalFolder(AUDIO_PATH);
        } else {
            return getAppDataFolder(AUDIO_PATH);
        }
    }

    /**
     * 音频保存、缓存地址
     *
     * @return
     */
    public static File getAudioDir() {
        return getAudioDir(isExternal, isPublic);
    }

    /**
     * 文档保存、缓存地址
     *
     * @return
     */
    public static File getDocDir(boolean external, boolean isPublic) {
        if (external) {
            if (isPublic) {
                return getExternalPackageDir(DOC_PATH);
            }
            return getAppExternalFolder(DOC_PATH);
        } else {
            return getAppDataFolder(DOC_PATH);
        }
    }

    /**
     * 文档保存、缓存地址
     *
     * @return
     */
    public static File getDocDir() {
        return getDocDir(isExternal, isPublic);
    }

    /**
     * 下载保存、缓存地址
     *
     * @return
     */
    public static File getDownLoadDir(boolean external, boolean isPublic) {
        if (external) {
            if (isPublic) {
                return getExternalPackageDir(DOWNLOAD_PATH);
            }
            return getAppExternalFolder(DOWNLOAD_PATH);
        } else {
            return getAppDataFolder(DOWNLOAD_PATH);
        }
    }

    /**
     * 下载保存、缓存地址
     *
     * @return
     */
    public static File getDownLoadDir() {
        return getDownLoadDir(isExternal, isPublic);
    }


    /**
     * Apk载文件保存地址
     *
     * @return
     */
    public static File getApkDir(boolean external, boolean isPublic) {
        if (external) {
            if (isPublic) {
                return getExternalPackageDir(APK_PATH_INSTALL);
            }
            return getAppExternalFolder(APK_PATH_INSTALL);
        } else {
            return getAppDataFolder(APK_PATH_INSTALL);
        }
    }

    /**
     * Apk载文件保存地址
     *
     * @return
     */
    public static File getApkDir() {
        return getApkDir(isExternal, isPublic);
    }


}
